//
//  WDHeadCell.m
//  Zhihuijinfu
//
//  Created by 王笛 on 2016/12/1.
//  Copyright © 2016年 王笛. All rights reserved.
//

#import "WDHeadCell.h"
#import "WDHead.h"

@interface WDHeadCell ()

/** 头像 */
@property(nonatomic, weak) UIImageView *iconView;
/** 姓名 */
@property(nonatomic, weak) UILabel *nameLabel;
/** 邮箱 */
@property(nonatomic, weak) UILabel *mailLabel;

@end

@implementation WDHeadCell

+ (instancetype)headCellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"Head";
    
    WDHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[WDHeadCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        //头像
        UIImageView *iconView = [[UIImageView alloc] init];
        [self.contentView addSubview:iconView];
        self.iconView = iconView;
        
        [iconView makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(49.5);
            make.left.equalTo(16);
            make.width.height.equalTo(40);
        }];
        iconView.layer.cornerRadius = 20;
        iconView.clipsToBounds = YES;
        
        //姓名
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.textColor = ColorForRGB(0xffffff);
        nameLabel.font = [UIFont systemFontOfSize:14];
        nameLabel.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:nameLabel];
        self.nameLabel = nameLabel;
        
        [nameLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(iconView.bottom).offset(5);
            make.left.equalTo(iconView.left);
        }];
        
        //邮箱
        UILabel *mailLabel = [[UILabel alloc] init];
        mailLabel.textColor = ColorForRGB(0xffffff);
        mailLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:mailLabel];
        self.mailLabel = mailLabel;
        
        [mailLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(nameLabel.bottom).offset(10);
            make.left.equalTo(nameLabel.left);
        }];
        
    }
    return self;
}

- (void)setHead:(WDHead *)head
{
    _head = head;
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:head.picture] placeholderImage:[UIImage imageNamed:@"defaultIcon"]];
    self.nameLabel.text = _head.name;
    self.mailLabel.text = _head.email_personal;
    
    [self layoutIfNeeded];
    
    head.cellH = CGRectGetMaxY(self.mailLabel.frame) + 30;
}

@end
